package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.UserDTO;
import com.quanquan.management.dto.UserGroupDTO;
import com.quanquan.management.request.UserGroupRequestBody;
import entity.ExcelData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
public class UserGroupService {

    @Value("${module-url.userUrl}")
    private String userUrl;

    public boolean create (UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/add",requestBody);
        if (StringUtil.isEmpty(json)) {
            return false;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public UserGroupDTO findById(String groupId) {
        UserGroupRequestBody requestBody = new UserGroupRequestBody();
        requestBody.setGroupId(groupId);
        String json = Request.postJson(userUrl + "user_group/get",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        UserGroupDTO userGroupDTO = null;
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            userGroupDTO = jsonObject.getJSONObject("data").toJavaObject(UserGroupDTO.class);
        }
        return userGroupDTO;
    }

    public boolean delete(String groupId) {
        UserGroupRequestBody requestBody = new UserGroupRequestBody();
        requestBody.setGroupId(groupId);
        String json = Request.postJson(userUrl + "user_group/delete_group",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean exists(String name) {
        UserGroupRequestBody requestBody = new UserGroupRequestBody();
        requestBody.setGroupName(name);
        String json = Request.postJson(userUrl + "user_group/getByName",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (-1 == resCode) {
            return true;
        }
        return false;
    }

    public boolean addUser(UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/select_user",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public List<String> findUserIdByGroupId(UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/query_group_member",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            List<String> data = jsonObject.getJSONArray("data").toJavaList(String.class);
            return data;
        }
        return Collections.emptyList();
    }

    public boolean pushCoupon(UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/group_push_coupon",requestBody);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return Integer.valueOf(0).equals(resCode);
    }

    public PageDTO<UserGroupDTO> list(UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/query", requestBody);
        PageDTO<UserGroupDTO> pageDTO = new PageDTO<>();
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<UserGroupDTO> userGroupDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                UserGroupDTO object = list.getObject(i, UserGroupDTO.class);
                userGroupDTOS.add(object);
            }
            data.setList(userGroupDTOS);
            return data;
        }
        return pageDTO;
    }

    public PageDTO<UserDTO> checkList(UserGroupRequestBody requestBody) {
        String json = Request.postJson(userUrl + "user_group/query_condition", requestBody);
        PageDTO<UserDTO> pageDTO = new PageDTO<>();
        JSONObject jsonObject = JSON.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<UserDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                UserDTO object = list.getObject(i, UserDTO.class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }


    public void exportUserGroup(HttpServletResponse response, UserGroupRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("用户名");
        titles.add("用户真实姓名");
        titles.add("会员卡号");
        titles.add("手机号");
        titles.add("积分");
        titles.add("创建时间");
        titles.add("生日");
        titles.add("性别");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<UserDTO> pageDTO = this.checkList(requestBody);
        List<UserDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getMemberName());
            row.add(vo.getName());
            row.add(vo.getMemberCardNum());
            row.add(vo.getPhone());
            row.add(vo.getIntegral());
            row.add(vo.getCreateTime());
            row.add(vo.getBirthday());
            row.add(vo.getGender()==0?"男":"女");
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"用户分组" + now,data);
    }

}
